﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wsitech.Model;
using Wsitech.BLL;
public partial class Backend_VideoAdd : BackendPageBase
{
    string sitemapid = HttpContext.Current.Request["sitemapid"] ?? "";
    string tid = HttpContext.Current.Request["tid"] ?? "";
    /// <summary>
    /// 获取栏目
    /// </summary>
    public SiteMapInfo SiteMap
    {
        get
        {
            SiteMapInfo map = null;
            if (!string.IsNullOrEmpty(sitemapid))
            {
                map = SiteMapInfoBLL.Instance.GetDetail(sitemapid);
            }
            else if (!string.IsNullOrEmpty(tid))
            {
                //如果是修改视频则SiteMapId应该是信息中的SiteMapId的父ID
                TextInfo info = TextInfoBLL.Instance.GetDetail(tid);
                SiteMapInfo childmap = SiteMapInfoBLL.Instance.GetDetail(info.SitemapId);
                if (childmap != null)
                {
                    map = SiteMapInfoBLL.Instance.GetDetail(childmap.ParentId);
                }

            }
            return map;
        }
    }
    /// <summary>
    /// 栏目名称
    /// </summary>
    public string SiteMapName
    {
        get
        {
            if (SiteMap != null)
            {
                return SiteMap.SitemapName;
            }
            else
            {
                return "";
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["back"] = Request.UrlReferrer.ToString();
            bindSiteMap();
            bindCompany();
            if (tid != "")
            {
                initVideoInfo(tid);
            }
        }
    }

    //初始化视频新闻
    private void initVideoInfo(string textinfoid)
    {
        TextInfo info = TextInfoBLL.Instance.GetDetail(textinfoid);
        if (info != null)
        {
            ddlCompany.SelectedValue = info.CompanyId;
            ddlSiteMap.SelectedValue = info.SitemapId;
            txtAdder.Text = info.Author;
            txtTitle.Text = info.Title;
            txtDescription.Text = info.Content;
            cbxIsTop.Checked = info.IsTop;
            txtPublishTime.Text = info.PublishTime.ToString("yyyy-MM-dd");

            //关键字
            txtKeyWords.Text = string.Join(",", KeywordBLL.Instance.GetListByTextInfoId(textinfoid).Select(k => k.Content));
        }
    }

    //绑定视频所属的栏目
    private void bindSiteMap()
    {
        if (SiteMap != null)
        {
            ddlSiteMap.DataSource = SiteMapInfoBLL.Instance.GetSiteMapByParentId(SiteMap.SitemapId);
            ddlSiteMap.DataTextField = "SitemapName";
            ddlSiteMap.DataValueField = "SitemapId";
            ddlSiteMap.DataBind();

            ddlSiteMap.Items.Insert(0, new ListItem("==请选择==", ""));
        }
    }
    //绑定公司信息
    private void bindCompany()
    {
        ddlCompany.DataSource = CompanyInfoBLL.Instance.GetList();
        ddlCompany.DataTextField = "CompanyName";
        ddlCompany.DataValueField = "CompanyId";
        ddlCompany.DataBind();

        ddlCompany.Items.Insert(0, new ListItem("=请选择=", "0"));
    }

    //提交信息
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (CheckParams())
        {
            //处理关键字
            string[] keys = txtKeyWords.Text.Trim().Replace('，', ',').Split(',');
            IList<String> keywords = new List<string>();
            foreach (string key in keys)
            {
                //关键字不重复且不为空
                if (key != "" && !keywords.Contains(key))
                {
                    keywords.Add(key);
                }
            }
            if (Request["tid"] == null)
            {
                TextInfoBLL.Instance.AddVideoInfo(
                    ddlSiteMap.SelectedValue,//所属栏目
                    ddlCompany.SelectedValue,
                    txtTitle.Text.Trim(),
                    keywords,
                    txtAdder.Text.Trim(),
                    cbxIsTop.Checked,
                    txtDescription.Text.Trim(),
                    DateTime.Parse(txtPublishTime.Text.Trim())
                    );
            }
            else
            {
                //编辑
                TextInfo info = TextInfoBLL.Instance.GetDetail(tid);
                if (info != null)
                {
                    info.SitemapId = ddlSiteMap.SelectedValue;//所属栏目
                    info.CompanyId = ddlCompany.SelectedValue;

                    info.Title = txtTitle.Text.Trim();
                    info.Author = txtAdder.Text.Trim();
                    info.IsTop = cbxIsTop.Checked;
                    info.Content = txtDescription.Text.Trim();
                    info.PublishTime = DateTime.Parse(txtPublishTime.Text.Trim());

                    info.UpdateTime = DateTime.Now;
                    TextInfoBLL.Instance.Update(info);

                    //关键字
                    TextInfoKeyRelaBLL.Instance.AddRela(info.TextInfoId, keywords);
                }
            }



            Response.Redirect(ViewState["back"].ToString());
        }

    }

    //数据验证
    private bool CheckParams()
    {
        //所属公司
        if (ddlCompany.SelectedIndex == 0)
        {
            base.Alert("请选择所属公司！");
            return false;
        }
        //所属栏目
        if (ddlSiteMap.SelectedIndex == 0)
        {
            base.Alert("请选择所属栏目！");
            return false;
        }
        //来源

        //if (txtAdder.Text.Trim() == "")
        //{
        //    Alert("来源不能为空！");
        //    return false;
        //}
        //else
        //{
        if (txtAdder.Text.Trim().Length > 30)
        {
            base.Alert("来源不能超过30字！");
            return false;
        }
        //}

        //标题
        if (txtTitle.Text.Trim() == "")
        {
            base.Alert("标题不能为空！");
            return false;
        }
        else
        {
            if (txtTitle.Text.Trim().Length > 100)
            {
                base.Alert("标题不超过100字！");
                return false;
            }
            else if (TextInfoBLL.Instance.TitleExists(txtTitle.Text.Trim(), tid))
            {
                base.Alert("该标题已经存在！");
                return false;
            }
        }
        //描述
        if (txtDescription.Text.Trim() == "")
        {
            base.Alert("描述不能为空！");
            return false;
        }
        else
        {
            if (txtDescription.Text.Trim().Length > 5000)
            {
                base.Alert("视频描述不超过5000字");
                return false;
            }
        }
        //发布时间
        try
        {
            DateTime.Parse(txtPublishTime.Text.Trim());
        }
        catch (Exception)
        {
            base.Alert("时间格式不正确！");
            return false;
        }
        return true;
    }
}